package com.erpanderp.wam.usm.view.user;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.swing.SwingWorker;

import com.erpanderp.wam.usm.model.entity.user.BirthDayWishes;
import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.usm.model.service.user.BirthDayWishesDAO;
import com.erpanderp.wam.usm.model.service.user.EmployeeDAO;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.EmailUtil;
import com.erpanderp.wam.util.WAMHostManager;

@ManagedBean
@ViewScoped
public class BirthDayWishesCronJobBean implements Serializable{

	
	private static final long serialVersionUID = 1L;
	ServletContext context = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
//	Session session = (Session)FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	private String cronName="Sent Birth Day Wishes through Cron Job";
	
	private List<Employee> employeeList = new ArrayList<Employee>();

	private Employee employee=new Employee();
	private BirthDayWishes birthDayWishes;
	
	private EmployeeDAO employeeDAO=new EmployeeDAO();
	private BirthDayWishesDAO birthDayWishesDAO=new BirthDayWishesDAO();
	
	private EmailUtil emailUtil;
	private String EMAIL_MESSAGE;
	private String employeeMail;
	private String employeeName;
	

	public BirthDayWishesCronJobBean() {
		
				
		employeeList = employeeDAO.getAllEmployeesByCurrentDate();
		
		for(Employee emp:employeeList){	
			employeeMail=emp.getEmail();
			employeeName=emp.getFirstName()+" "+emp.getLastName();
			birthDayWishes=new BirthDayWishes();
			birthDayWishes.setWishMessage("May this year bring with it all the success and fulfillment your heart desires. Wishing you all the great things in life, hope this day will bring you an extra share of all that makes you happiest and may all the wishes and dreams you dream today turn to reality.Happy Birthday.");
			birthDayWishes.setEmployee(emp);
			birthDayWishes.setSentOn(new Date());
			birthDayWishes.setEmployeeName(emp.getFirstName()+" "+emp.getLastName());
			
			
			birthDayWishes.setCreateDate(new Date());
			birthDayWishes.setCreatedBy("ERP Admin");
			birthDayWishes.setModifiedBy("ERP Admin");
			birthDayWishes.setStatus(CommonLookup.STATUS_ACTIVE);
			
			birthDayWishesDAO.saveOrUpdateBirthDayWishes(birthDayWishes);
			
			// MAIL TRIGGERING CODE
			System.out.println("AT CALLER METHOD: " + sendMessage()); // *******DONT REMOVE*******
			}
		
	}
	
	//RECOMMEND MAIL
			public String sendMessage() {
				Map<String, String> hostsMap = WAMHostManager.getWAMHosts(context);
				String domainName = context.getInitParameter("domainName");
				String messageString = "";
				
				emailUtil = new EmailUtil();
				if (hostsMap.get(CommonLookup.DEV_EMAIL) == null) {
					String[] to = { employeeMail };
					emailUtil.setRecieverAddresses(to);			
				} else {
					String[] to = { hostsMap.get(CommonLookup.DEV_EMAIL) };
					emailUtil.setRecieverAddresses(to);			
				}				

				messageString = "<font face='comic sans MS, new york, times' size='2' color='#00A2DE'><b>Dear " + employeeName + ",</b></font><br /><br />" +
						
						"<font size='3' face='Lucida Calligraphy' color='#F5A508'><b><p>May this year bring with it all the success and fulfillment your heart desires.</p></b></font>"+
						
						"<font size='3' face='Lucida Calligraphy' color='#F5A508'><b><p>Wishing you all the great things in life, hope this day will bring you an extra share of all that makes you happiest and may all the wishes and dreams you dream today turn to reality.</p></b></font>"+
						
												
						"<font size='3' face='Lucida Calligraphy' color='#F5A508'><b><p>Happy Birthday.</b></p></font>"+

						"<font face='comic sans MS, new york, times' size='2' color='#00A2DE'><b>Thanks & Regards, <br />ERPandERP India Pvt Ltd </b></font>";
				
				
				emailUtil.setMessage(messageString);
				emailUtil.setSenderEmail("podquick@erpanderp.com", "WAM - Dev Team");
				emailUtil.setSubject("** Many Many Happy Return of the Day **");

				SwingWorker worker = new SwingWorker<String, Object>() {
					@Override
					protected String doInBackground() throws Exception {
						EMAIL_MESSAGE = emailUtil.processAndSendEmail();
						return null;
					}

					@Override
					protected void done() {
						System.out.println("DONE " + EMAIL_MESSAGE);
					}
				};
				worker.execute();
				System.out.println("BEFORE RETURNING: " + EMAIL_MESSAGE);
				return EMAIL_MESSAGE;
			}
			

	//SETTERS AND GETTERS
	public String getCronName() {
		return cronName;
	}

	public void setCronName(String cronName) {
		this.cronName = cronName;
	}

	public List<Employee> getEmployeeList() {
		return employeeList;
	}

	public void setEmployeeList(List<Employee> employeeList) {
		this.employeeList = employeeList;
	}


	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public EmployeeDAO getEmployeeDAO() {
		return employeeDAO;
	}

	public void setEmployeeDAO(EmployeeDAO employeeDAO) {
		this.employeeDAO = employeeDAO;
	}

	public BirthDayWishes getBirthDayWishes() {
		return birthDayWishes;
	}

	public void setBirthDayWishes(BirthDayWishes birthDayWishes) {
		this.birthDayWishes = birthDayWishes;
	}

	public BirthDayWishesDAO getBirthDayWishesDAO() {
		return birthDayWishesDAO;
	}

	public void setBirthDayWishesDAO(BirthDayWishesDAO birthDayWishesDAO) {
		this.birthDayWishesDAO = birthDayWishesDAO;
	}

	public String getEmployeeMail() {
		return employeeMail;
	}

	public void setEmployeeMail(String employeeMail) {
		this.employeeMail = employeeMail;
	}

	public String getEmployeeName() {
		return employeeName;
	}

	public void setEmployeeName(String employeeName) {
		this.employeeName = employeeName;
	}

}
